<?php
echo "=== WebSocket服务器环境检查 ===\n\n";

// 1. 检查PHP版本
echo "1. PHP版本检查:\n";
echo "   当前版本: " . PHP_VERSION . "\n";
if (version_compare(PHP_VERSION, '7.0.0', '>=')) {
    echo "   ✓ PHP版本符合要求 (>= 7.0)\n";
} else {
    echo "   ✗ PHP版本过低，需要 >= 7.0\n";
}
echo "\n";

// 2. 检查必需的PHP扩展
echo "2. PHP扩展检查:\n";
$extensions = [
    'sockets' => '创建和管理Socket连接',
    'json' => '处理JSON数据',
    'pdo' => '数据库连接',
    'pcntl' => '进程控制 (可选)',
    'posix' => 'POSIX函数 (可选)'
];

foreach ($extensions as $ext => $desc) {
    if (extension_loaded($ext)) {
        echo "   ✓ {$ext} - 已加载 ({$desc})\n";
    } else {
        if (in_array($ext, ['pcntl', 'posix'])) {
            echo "   ? {$ext} - 未加载 ({$desc}) [可选]\n";
        } else {
            echo "   ✗ {$ext} - 未加载 ({$desc}) [必需]\n";
        }
    }
}
echo "\n";

// 3. 检查Socket函数
echo "3. Socket函数检查:\n";
$socket_functions = [
    'socket_create',
    'socket_bind', 
    'socket_listen',
    'socket_accept',
    'socket_select',
    'socket_read',
    'socket_write',
    'socket_close'
];

foreach ($socket_functions as $func) {
    if (function_exists($func)) {
        echo "   ✓ {$func}\n";
    } else {
        echo "   ✗ {$func} - 不可用\n";
    }
}
echo "\n";

// 4. 检查端口可用性
echo "4. 端口可用性检查:\n";
$test_ports = [8080, 8081, 8082, 8083, 8084];
$available_ports = [];

foreach ($test_ports as $port) {
    $socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ($socket) {
        socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
        if (@socket_bind($socket, '0.0.0.0', $port)) {
            echo "   ✓ 端口 {$port} - 可用\n";
            $available_ports[] = $port;
        } else {
            echo "   ✗ 端口 {$port} - 被占用或无权限\n";
        }
        socket_close($socket);
    } else {
        echo "   ✗ 端口 {$port} - 无法创建Socket\n";
    }
}
echo "\n";

// 5. 检查文件权限
echo "5. 文件权限检查:\n";
$files = [
    'config/database.php',
    'includes/functions.php', 
    'websocket_server.php'
];

foreach ($files as $file) {
    if (file_exists($file)) {
        if (is_readable($file)) {
            echo "   ✓ {$file} - 存在且可读\n";
        } else {
            echo "   ✗ {$file} - 存在但不可读\n";
        }
    } else {
        echo "   ✗ {$file} - 文件不存在\n";
    }
}
echo "\n";

// 6. 检查日志目录
echo "6. 日志目录检查:\n";
$log_dir = __DIR__ . '/logs';
if (!is_dir($log_dir)) {
    if (mkdir($log_dir, 0755, true)) {
        echo "   ✓ 日志目录创建成功: {$log_dir}\n";
    } else {
        echo "   ✗ 无法创建日志目录: {$log_dir}\n";
    }
} else {
    if (is_writable($log_dir)) {
        echo "   ✓ 日志目录可写: {$log_dir}\n";
    } else {
        echo "   ✗ 日志目录不可写: {$log_dir}\n";
    }
}
echo "\n";

// 7. 检查数据库连接
echo "7. 数据库连接检查:\n";
try {
    if (file_exists('config/database.php')) {
        require_once 'config/database.php';
        echo "   ✓ 数据库配置文件加载成功\n";
        
        // 尝试连接数据库
        if (isset($pdo)) {
            echo "   ✓ 数据库连接成功\n";
        } else {
            echo "   ✗ 数据库连接对象未创建\n";
        }
    } else {
        echo "   ✗ 数据库配置文件不存在\n";
    }
} catch (Exception $e) {
    echo "   ✗ 数据库连接失败: " . $e->getMessage() . "\n";
}
echo "\n";

// 8. 总结和建议
echo "8. 总结和建议:\n";
if (extension_loaded('sockets') && !empty($available_ports)) {
    echo "   ✓ 环境基本满足WebSocket服务器运行条件\n";
    echo "   建议使用端口: " . $available_ports[0] . "\n";
    echo "\n";
    echo "启动命令:\n";
    echo "   php start_websocket_debug.php\n";
    echo "   或者\n";
    echo "   php websocket_server.php\n";
} else {
    echo "   ✗ 环境不满足运行条件\n";
    if (!extension_loaded('sockets')) {
        echo "   请安装 sockets 扩展: \n";
        echo "   - Ubuntu/Debian: sudo apt-get install php-sockets\n";
        echo "   - CentOS/RHEL: sudo yum install php-sockets\n";
        echo "   - 或者编译时添加 --enable-sockets\n";
    }
    if (empty($available_ports)) {
        echo "   请检查端口权限或更改为其他端口\n";
    }
}

echo "\n=== 检查完成 ===\n";
?>
